package ch.fusun.baron.swt.isometry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ch.fusun.baron.swt.isometry.components.IsometryWidget;
import ch.fusun.baron.swt.isometry.components.Sprite;

/**
 * Test class
 */
public class Starter {
	private static final int SIZE = 20;
	static int[][] heightMap = new int[SIZE * 2][SIZE * 2];

	/**
	 * @param args
	 *            console arguments
	 */
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());

		IsometryWidget isometryWidget = new IsometryWidget(shell, SWT.NONE,
				SIZE, SIZE, 1, 64, 64, 32);

		for (int i = 0; i < heightMap.length; i++) {
			for (int j = 0; j < heightMap[i].length; j++) {
				heightMap[i][j] = Math.random() < 0.5 ? 1 : 0;
			}
		}

		for (int i = 0; i < SIZE; i++) {
			for (int j = 0; j < SIZE; j++) {
				isometryWidget.addItem(i + ", " + j, i, j, //$NON-NLS-1$
						getSprite(getID(i, j, heightMap), display));
			}
		}

		isometryWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		shell.setText("Map"); //$NON-NLS-1$
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	private static Sprite getSprite(String id, Display display) {
		return new Sprite(new Image(display, id + ".png"), new Image(display, //$NON-NLS-1$
				id + "_mask.png")); //$NON-NLS-1$
	}

	/**
	 * @param i
	 *            i coordinate
	 * @param j
	 *            j coordinate
	 * @param heightMap
	 *            The heightMap
	 * @return The id generated from position and height map
	 */
	protected static String getID(int i, int j, int[][] heightMap) {
		long result = 0;
		result = heightMap[i][j] + 2 * heightMap[i + 1][j] + 4
				* heightMap[i + 1][j + 1] + 8 * heightMap[i][j + 1];

		return new Long(result).toString();
	}

}
